<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <!--
        el—form :form="表单对象" :rule="规则"
        el-form-item prop="指定哪一个规则"
        el-input v-model="双向绑定"
      -->
      <el-form ref="form" :rules="rules" :model="form">
        <el-form-item
          label="账号"
          prop="username"
        >
          <el-input v-model="form.username" />
        </el-form-item>

        <el-form-item
          label="密码"
          prop="password"
        >
          <el-input v-model="form.password" />
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model="remember">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login_btn" @click="LoginHandle">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
const REMEMER_KEY = 'remember_key'
export default {
  name: 'Login',
  data() {
    return {
      // 是否选中记住按钮
      remember: false,
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  // 功能---点击完（记得密码）的选项之后，通过localStorage中获取账号和密码并，填入form表单中
  created() {
    // 初始化获取本地的账号和密码
    const form = JSON.parse(localStorage.getItem(REMEMER_KEY))
    // 如果存在就存入form表单里面
    if (form) {
      this.form = form
      // 有值的那么remember的状态也要进行改变
      this.remember = true
    }
  },
  methods: {
    // 登录请求
    async LoginHandle() {
      // 对所有字段进行校验
      this.$refs.form.validate(async valid => {
        // 先判断用户是否点击了（记住我）
        if (this.remember) {
          // 如果进行点了（记住我）表单数据存储到本地
          localStorage.setItem(REMEMER_KEY, JSON.stringify(this.form))
        } else {
          // 如果没有进行点了 （记住我）讲本地的localStorage删除
          localStorage.removeItem(REMEMER_KEY)
        }
        // 如果字段校验成功则对接口发起请求
        await this.$store.dispatch('user/ActionLogin', this.form)
        // 成功则跳转到登录界面
        this.$router.push('/')
        // 提示框
        this.$message.success('欢迎您登录')
      })
    }
  }

}

</script>

<style scoped lang="scss">
  .login_body {
    display: flex;
  }
  .bg {
    width: 60vw;
    height: 100vh;
    background: url('~@/assets/login-bg.svg') no-repeat;
    background-position: right top;
    background-size: cover;
  }
  .box {
    margin: 200px 10% 0;
    flex: 1;
    .title {
      padding-bottom: 76px;
      font-size: 26px;
      font-weight: 500;
      color: #1e2023;
    }
    ::v-deep() {
      .ant-form-item {
        display: flex;
        margin-bottom: 62px;
        flex-direction: column;
      }
      .ant-form-item-label label {
        font-size: 16px;
        color: #8b929d;
      }
      .ant-input,
      .ant-input-password {
        border-radius: 8px;
      }
    }
  }
  .login_btn{
    width: 100%;
  }
</style>
